package cn.weisoft.nconv.service.impl;

import cn.weisoft.nconv.mapper.GardenMapper;
import cn.weisoft.nconv.model.Garden;
import cn.weisoft.nconv.model.GardenPara;
import cn.weisoft.nconv.model.GardenRet;
import cn.weisoft.nconv.service.GardenService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GardenServiceImpl extends ServiceImpl<GardenMapper,Garden> implements GardenService {

    @Autowired
    private GardenMapper gardenMapper;

    @Override
    public List<Garden> selectGardenByBnd(float minLng, float minLat, float maxLng, float maxLat) {
        QueryWrapper<Garden> wrapper = new QueryWrapper<>();
        wrapper.between("lng",minLng,maxLng).or().between("lat",minLat,maxLat);
        return gardenMapper.selectList(wrapper);
    }

    @Override
    public GardenRet selectPage(GardenPara para) {
        GardenRet ret = new GardenRet();

        System.out.println("para:" + para.toString());

        Page<Garden> page = new Page<>(para.getCurrentIndex(), para.getPageSize());
        page.addOrder(OrderItem.desc("id"), OrderItem.desc("title"));
        QueryWrapper<Garden> wrapper = new QueryWrapper<>();

        wrapper.like(StringUtils.isNotBlank(para.getTitle()),"title",para.getTitle())
                .like(StringUtils.isNotBlank(para.getAddress()),"address",para.getAddress());


        Page<Garden> gardenPage = gardenMapper.selectPage(page, wrapper);
        System.out.println("total:" +  page.getTotal());


        ret.setCount(gardenPage.getTotal());
        ret.setList(gardenPage.getRecords());
        return ret;
    }
}
